var vm = new Vue({ 
    el: '#container' 
    , data: { 
        buttons: {}
        , query: { 
            page: 1 
            , page_size: hcCore.pageSize 
            , roleNo:'' 
            , roleName:'' 
        } 
        , tableData: { 
            records: [] 
            , total: 0 
            , selectedCount: 0
        } 
        , powerInfo: {
            data: []
            , roleNo: ''
            , visible: false
            , checked: []
            , value: []
        }
        , edit: { 
            formId: 'editFrame' 
            , title: '操作' 
            , visible: false 
            , fields: [{ 
                label: '角色编号' 
                , name: 'role_no' 
                , require: true 
                , type: 'text' 
                , disabled: false 
                , value: '' 
                , placeholder: '请输入角色编号' 
                , rules: { 
                    required: true, message: '角色编号不能为空', trigger: 'blur' 
                } 
            },{ 
                label: '角色名称' 
                , name: 'role_name' 
                , require: true 
                , type: 'text' 
                , disabled: false 
                , value: '' 
                , placeholder: '请输入角色名称' 
                , rules: { 
                    required: true, message: '角色名称不能为空', trigger: 'blur' 
                } 
            },{ 
                label: '角色描述' 
                , name: 'role_desc' 
                , require: true 
                , type: 'text' 
                , disabled: false 
                , value: '' 
                , placeholder: '请输入角色描述' 
                , rules: { 
                    required: true, message: '角色描述不能为空', trigger: 'blur' 
                } 
            }] 
            , op: function () { 
            } 
        } 
    }
    , mounted: async function () { 
        const me = this; 
        me.buttons = await hcCore.getPowerButton();
        if (me.buttons.hasQuery) {
            me.onQuery();
        }
    } 
    , methods: { 
        handleButton: function (actNo, row) {
            switch (actNo) {
                case 'Query':
                    this.onQuery();
                    break;
                case 'Add':
                    this.showEdit('add', '新增');
                    break;
                case 'Edit':
                    this.showEdit('edit', '编辑', row);
                    break;
                case 'Power':
                    this.onPower(row);
                    break;
                case 'Delete':
                    this.onDelete(row);
                    break;
                default:
                    break;
            }
        }
        , onQuery: function () { 
            let me = this; 
            hcCore.post({ 
                url: '/SysRole/List' 
                , param: { 
                    page: me.query.page 
                    , page_size: me.query.page_size 
                    , role_no: me.query.roleNo 
                    , role_name: me.query.roleName 
                } 
                , success: function (rsp) { 
                    me.tableData.records = rsp.data; 
                    me.tableData.total = rsp.total; 
                } 
            }) 
        } 
        , onPageChange: function (page) {
            this.query.page = page;
            this.onQuery();
        } 
        , showEdit: async function (type, title, row) { 
            let me = this;
            me.resetParams(me.edit.fields);
            me.edit.title = title; 
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { }); 
            if (type == 'edit') {
                if (row) { 
                    await hcCore.post({ 
                        url: '/SysRole/Get/' + row.role_no 
                        , success: function (rsp) { 
                            me.edit.fields.forEach((item, index) => { 
                                if (rsp.data[item.name]) { 
                                    item.value = rsp.data[item.name]; 
                                } 
 
                                if (item.name == 'role_no') { 
                                    item.disabled = true; 
                                } 
                            }); 
                        } 
                        , fault: '获得数据失败' 
                    }); 
                } 
                else { 
                    hcCore.showError('请选择要编辑的记录'); 
                    return; 
                } 
            } else { 
 
            } 
            me.edit.visible = true; 
        } 
        , onInsert: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysRole/Insert' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('新增成功'); 
                            me.tableData.records.push(rsp.data); 
                            me.tableData.total += 1; 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onUpdate: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysRole/Update' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('更新成功'); 
                            let index = me.tableData.records.findIndex(item => item.role_no == rsp.data.role_no); 
                            me.$set(me.tableData.records, index, rsp.data) 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onDelete: function (row) { 
            let me = this; 
            let rows = [];
            if (row) {
                rows = [row];
            }
            else {
                rows = me.$refs.tblRecords.selection;
            }
            if (rows && rows.length > 0) { 
                let tips = rows.length == 1 ? ('确认要删除【' + rows[0].role_no + '】这条记录吗') : ('确认要删除选择【' + rows.length + '】条的记录吗？')
                hcCore.confirm(tips, function () { 
                    let arr = rows.map(item => { return item.role_no }); 
                    let nos = arr.join(','); 
                    hcCore.post({ 
                        url: '/SysRole/Delete' 
                        , param: { 
                            role_nos: nos 
                        } 
                        , success: function (rsp) { 
                            hcCore.showSuccess('删除成功'); 
                            arr.forEach(item => { 
                                let index = me.tableData.records.findIndex(node => item == node.role_no); 
                                me.tableData.records.splice(index, 1); 
                            }); 
                        } 
                    }); 
                }); 
            } 
            else { 
                hcCore.showError('请选择要删除的记录'); 
            } 
        }
        , onPower: async function (row) {
            let me = this;
            me.powerInfo.checked = [];
            if (!row) {
                hcCore.showError('请选择要操作的记录');
                return;
            }
            me.powerInfo.roleNo = row.role_no;
            await hcCore.post({
                url: '/SysRole/Tree'
                , param: { p_no: '-' }
                , success: async function (rsp) {
                    me.powerInfo.data = rsp.data;
                    await me.getPowers();
                }
            });
        }
        , resetParams: function (obj) {
            obj.forEach(item => {
                item.value = '';
                item.disabled = false;
            });
        }
        , onRowSelect: function () {
            this.tableData.selectedCount = this.$refs.tblRecords.selection.length;
        }
        , getPowers: async function () {
            let me = this;
            await hcCore.post({
                url: '/SysRole/MenusAndActions/' + me.powerInfo.roleNo
                , success: function (rsp) {
                    me.powerInfo.checked = rsp.data.filter(item => item.indexOf('|') > -1);
                    me.powerInfo.visible = true;
                }
            });
        }
        , grantPowers: function () {
            let me = this;
            let selectedNodes = this.$refs.tree.getCheckedNodes(false, true);
            let keys = [];
            if (selectedNodes && selectedNodes.length > 0) {
                keys = selectedNodes.map(item => item.menu_no);
            }
            hcCore.post({
                url: '/SysRole/GrantMenus/' + me.powerInfo.roleNo
                , param: { menu_nos: keys }
                , success: function (rsp) {
                    hcCore.showSuccess('操作成功');
                }
            });
        }
        , getParams: function () { 
            let param = {}; 
            this.edit.fields.forEach((item, index) => { 
                param[item.name] = item.value; 
            }); 
            return param; 
        } 
        , recordIndex: function (index) {
            return (this.query.page - 1) * this.query.page_size + index + 1;
        }
    } 
});